package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bean.User;
import dao.CheckSellUserInfo;
import dao.SelectOneUser;

/**
 * Servlet Filter implementation class UserInfoGet 在进入用户界面和修改界面时候获取用户信息
 */
public class UserInfoGet implements Filter {

	/**
	 * Default constructor.
	 */
	public UserInfoGet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpSession session = httpServletRequest.getSession();
		String numString = (String) session.getAttribute("usernum");// 取得session中得字段信息

		if (CheckSellUserInfo.checkInfo(numString)) {
			User user = SelectOneUser.getOneUser(numString);
			request.setAttribute("user", user);
		} else {
			request.getRequestDispatcher("/user/update.jsp").forward(request,
					response);
			return;
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
